package com.timothy.calculate;


public class CalculatorUtil {

	public final static long MAX_YEAR_INVEST = 1000000;
	public final static long MIN_YEAR_INVEST = 10000;

    public final static int MAX_YEAR = 1;
    public final static int MIN_YEAR = 100;

    public final static double MAX_YEAR_RETURN = 1;
    public final static double MIN_YEAR_RETURN = 0.01;

	public final static double MAX_RETURN = 1.0;

	public static int getYear(double yearInvest,double yearlyReturn){
//        System.out.println("-------getYear-------");
//        System.out.println("incomePerYear:"+incomePerYear);
//        System.out.println("yearlyReturn:"+yearlyReturn);
        int year = 0;
		double money = 0;
		while(money< MAX_YEAR_INVEST){
            System.out.println("Y "+year+ " " + money);
            money = (money + yearInvest) * ( 1 + yearlyReturn) ;
			year ++;
		}
//        System.out.println("-------getYear-------");
        return year;
	}

    public static double getYearInvest(int targetYear,double yearReturn){
        System.out.println("-------getYearInvest-------");
        int iniYearInvest = (int) MAX_YEAR_INVEST /targetYear;
        System.out.println("targetYear:"+targetYear);
        System.out.println("yearReturn:"+yearReturn);
        System.out.println("iniYearInvest:"+iniYearInvest);

        double yearInvest;
        if(iniYearInvest%1000>0){
            yearInvest = iniYearInvest - iniYearInvest%1000 + 1000 ;
        }else{
            yearInvest = iniYearInvest;
        }
        while (getYear(yearInvest,yearReturn)< targetYear){
            System.out.println(yearInvest+ " " + getYear(yearInvest,yearReturn));
            yearInvest = yearInvest -1000;
        }

        System.out.println("-------getYearInvest-------");
        return yearInvest;
    }

    /**
     * range(1%-100%)
     * @param targetYear(1-100)
     * @param yearInvest(10,000-1000,000)
     * @return
     */
    public static double getYearReturn(int targetYear,double yearInvest){
        System.out.println("-------getYearReturn-------");
        double yr = 0.01;
        while (getYear(yearInvest,yr) > targetYear){
            System.out.println(yr);
            yr = yr + 0.01;
        }
        System.out.println("-------getYearReturn-------");
        return yr;
    }

    public static double getLeastInvestByYear(int year){
        double least = 10000;
        while(getYear(least,MAX_RETURN) > year){
            least+= 10000;
//            System.out.println(least);
        }
        return least;
    }
}
